/*
    This file is part of Posixcpp library.

    Posixcpp library is free software: you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public License
    as published by the Free Software Foundation, either version 3 of
    the License, or (at your option) any later version.

    Posixcpp library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU LesserGeneral Public
    License along with Posixcpp library. If not, see
    <http://www.gnu.org/licenses/>.
*/

/**
 * @file posix_mutex.hpp
 * @brief POSIX timespec
 */

#ifndef POSIX_TIME_TIMESPEC_INCLUDED
#define POSIX_TIME_TIMESPEC_INCLUDED

#include <ostream>
#include <ctime>
#include <sys/time.h>

namespace posix
{
namespace time
{

extern const int64_t SEC_2_mSEC;
extern const int64_t SEC_2_uSEC;
extern const int64_t SEC_2_nSEC;

/**
 * @brief Time value object
 */
class Timespec final
{
public:
	Timespec();
	/**
	 * Constructor from POSIX.1-2008 timespec
	 */
	Timespec(const timespec & data);
	/**
	 * Constructor POSIX.1-2008 timeval
	 */
	Timespec(const timeval & data);
	Timespec(time_t sec, long nsec);
	Timespec(int64_t nsec);

	timespec &data();
	const timespec &data() const;

	time_t sec() const;
	long nsec() const;

	Timespec & operator +=(const Timespec & other);
	Timespec & operator -=(const Timespec & other);

	Timespec operator +(const Timespec & other) const;
	Timespec operator -(const Timespec & other) const;

	bool operator >(const Timespec & other) const;
	bool operator <(const Timespec & other) const;

    operator int64_t();

    friend std::ostream & operator <<(std::ostream & stream,
                                      const Timespec & item);
private:
	timespec m_data;
};
}
}

#endif // POSIX_TIME_TIMESPEC_INCLUDED
